import TimeSpan from "./TimeSpan";

export class DateUtils {
    public static platform(): string {
        const agent = navigator.userAgent.toLowerCase();
        const android = agent.indexOf("android");
        const ios = agent.indexOf("iphone");
        const ipad = agent.indexOf("ipad");
        let macos = agent.indexOf("macintosh");
        if (macos === -1) {
            macos = agent.indexOf("safari");
            if (macos === -1) macos = agent.indexOf("applewebkit");
        }
        if (android !== -1) return "android";
        if (ios !== -1 || ipad !== -1 || macos !== -1) return "ios";
        return "";
    }

    // public static isIOS() {
    //   if (DateUtils.platform() === "ios") return true;
    //   return false;
    // }

    public static isIOS(): boolean {
        switch (uni.getSystemInfoSync().platform) {
            case "android":
                //console.log("运行Android上");

                break;

            case "ios":
                //console.log("运行iOS上");
                return true;
                break;

            default:
                //console.log("运行在开发者工具上");

                break;
        }
        return false;
    }

    /**
     @strDate: just lik 2008-10-11 12:03:33
     @return: Date
     */
    public static str2Date(strDate: string): Date {
        if (DateUtils.isIOS()) {
            const t = strDate.indexOf("T");
            if (t === -1) {
                const cc = strDate.split(" ");
                if (cc && cc.length === 2) {
                    strDate = cc[0] + "T" + cc[1];
                }
            }
        }
        return new Date(strDate);
    }

    public static diffNow(strDate: string): TimeSpan {
        let dn = new Date();
        let d = DateUtils.str2Date(strDate);
        let df: TimeSpan = TimeSpan.fromDates(d, dn);
        return df;
    }

    public static twoN(d: number): string {
        if (d >= 10) return d.toString();
        return "0" + d.toString();
    }

    public static tmdhhss(strTime: string): string {
        const dt = DateUtils.str2Date(strTime);
        const m = dt.getMonth() + 1;
        const d = dt.getDate();
        const hh = DateUtils.twoN(dt.getHours());
        const mm = DateUtils.twoN(dt.getMinutes());

        return `${m}月${d}日 ${hh}:${mm}`;
    }

    public static dateToString(date: any) {
        var year = date.getFullYear();
        var month = (date.getMonth() + 1).toString();
        var day = (date.getDate()).toString();
        if (month.length == 1) {
            month = "0" + month;
        }
        if (day.length == 1) {
            day = "0" + day;
        }
        var dateTime = year + "-" + month + "-" + day;
        return dateTime;
    }

    public static formatYear(strTime: string): any {
        const dt = DateUtils.str2Date(strTime);
        const y = dt.getFullYear();

        return y;
    }

    static getFormatDate() {
        var date = new Date();
        var month: any = date.getMonth() + 1;
        var strDate: any = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentDate = date.getFullYear() + "-" + month + "-" + strDate
            + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
        return currentDate;
    }


}
